//
//  HomeViewController.swift
//  weibo
//
//  Created by Fly on 2017/12/11.
//  Copyright © 2017年 Fly. All rights reserved.
//

import UIKit

// MARK: 定义全局变量，最好用 private 修饰，否则其他文件都能访问到
private let cellID = "CELLID"

class HomeViewController: BaseViewController {
    
    fileprivate lazy var listViewModel = StatusListViewModel()
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
    
    }

//    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//        return dataArray.count
//    }
    
    // MARK: 子类如果需要重写父类的方法，需看此方法在父类中的Class中还是extension中，若此方法在父类中的Class中，则子类重写时需要在自身的Class中重写，若写在自身的extension中，则不会执行；若此方法在父类的extension中，则子类重写此方法需写在自身的extension中
//    override func loadData() {
//        
//        for i in 0 ..< 10 {
//            
//            dataArray.insert(i.description, at: 0)
//            
//        }
//        
//    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

extension HomeViewController {

    /// 重写父类方法
    override func setupChildUI() {
        
        super.setupChildUI()
        
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友",target: self, action: #selector(showFriends))
        
        // 注册cell
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellID)
        
        setupNavTitle()
        
    }
    
    /// 设置导航栏标题
    private func setupNavTitle() {
        
        let titleStr = NetworkManager.networkManager.userAccount.screen_name
        
//        let button = UIButton.easyBtn(title: titleStr, fontSize: 17.0, titleNormalColor: UIColor.darkGray)
//
//        button.setImage(#imageLiteral(resourceName: "down"), for: [])
//
//        button.setImage(#imageLiteral(resourceName: "up"), for: .selected)
        
        let button = TitleBtn(title: titleStr)
        
        button.addTarget(self, action: #selector(titleBtnClicked), for: .touchUpInside)
        
        navItem.titleView = button
        
    }
    
    @objc func titleBtnClicked(send:UIButton) {
        
        send.isSelected = !send.isSelected
        
    }
    
    /// 加载数据
    override func loadData() {
          
//        NetworkManager.networkManager.statusList { (list, isSuccess) in
//            print(list as Any)
//        }
        
        // 模拟延迟加载数据（秒） 
//        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
//            
//            for i in 0 ..< 10 {
//                
//                if self.isPullup == true {// 上拉刷新
//                
//                    self.dataArray.append("上拉" + String(i))
//                    
//                }
//                else {// 下拉刷新
//                
//                    self.dataArray.insert(i.description, at: 0)
//                
//                }
//                
//            }
//            
//            self.refreshControl?.endRefreshing()
//            
//            // 恢复上拉刷新的标记
//            self.isPullup = false
//            
//            self.tableView?.reloadData()
//            
//        }
        
        listViewModel.loadStatus(pullUp: self.isPullup)  { (isSuccess, hasMorePullup) in
            
            self.refreshControl?.endRefreshing()
            
            // 恢复上拉刷新的标记
            self.isPullup = false
            
            if hasMorePullup {
            
                self.tableView?.reloadData()
                
            }
            
        }
        
    }
    
    /// 显示好友
    @objc private func showFriends() {
        
        let vc = FriendsViewController()
        
        navigationController?.pushViewController(vc, animated: true)
    
    }

}

// MARK: tableView的代理方法
extension HomeViewController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return listViewModel.statusList.count
        
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath)
        
//        guard let model: StatusModel = listViewModel.statusList[indexPath.row] else {
//            
//            return
//
//        }
        
        let model: StatusModel = listViewModel.statusList[indexPath.row]
        
        cell.textLabel?.text = model.text
        
        cell.backgroundColor = UIColor.randomColor
        
        return cell
    }
    
}
